<?php

/*
 * Following code will list all the game
 */

// array for JSON response
$response = array();


if (isset($_POST['user_id'])) {
    $user_id = $_POST['user_id'];

// include db connect class
require_once __DIR__ . '/db_connect.php';

// connecting to db
$db = new DB_CONNECT();

// get all game from game table
$result = mysql_query("SELECT * FROM game WHERE user_id = $user_id ") or die(mysql_error());

// check for empty result
if (mysql_num_rows($result) > 0) {
    // looping through all results
    // game node
    $response["game"] = array();
    
    while ($row = mysql_fetch_array($result)) {
        // temp user array
        $game = array();
        $game["id"] = $row["id"];
        $game["user_id"] = $row["user_id"];
        $game["id_level"] = $row["id_level"];
        $game["rate"] = $row["rate"];
        $game["score"] = $row["score"];


        // push single product into final response array
        array_push($response["game"], $game);
    }
    // success
    $response["responseCode"] = 1;

    // echoing JSON response
    echo json_encode($response);
} else {
    // no game found
    $response["responseCode"] = -1;
    $response["responseMessage"] = "No game found";

    // echo no users JSON
    echo json_encode($response);
}
}
else {
    // required field is missing
    $response["responseCode"] = -1;
    $response["responseMessage"] = "Required field(s) is missing";

    // echoing JSON response
    echo json_encode($response);
}
?>
